/*
 *  Cero Project - Copyright   2006 The Cero Developement Team 
 *  (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade) 
 * 
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the Free 
 *  Software Foundation; either version 2 of the License, or (at your option) 
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 */

package org.ceroproject;

import java.util.ArrayList;
import java.util.List;

/**
 * This interface provides functions for identifying objects
 * 
 * @author Telem
 */
public abstract class Identifiable {

	/** The next ID that will be provided to the next new identifiable object */
	private static int nextID = 0;

	/** The list of the identifiable objects */
	private static List<Object> IDlist = new ArrayList<Object>();

	/**
	 * Provides a new unique id
	 * 
	 * @return a new unique id
	 */
	private void setID() {
		myID = nextID++;
		IDlist.add(myID, this);
	}

	/** The id of this object */
	private int myID;

	/**
	 * Constructs a new object identified by a unique id
	 */
	public Identifiable() {
		setID();
	}

	/**
	 * Returns this Object's unique ID
	 * 
	 * @return a unique ID
	 */
	public int getId() {
		return myID;
	}

	/**
	 * Returns the object wearing this ID
	 * 
	 * @param objectID
	 *            the ID of the object to find
	 * @return the object corresponding to the ID objectID, or null if no
	 *         objects matches
	 */
	public static Object getObject(int objectID) {
		try {
			return IDlist.get(objectID);
		} catch (IndexOutOfBoundsException e) {
			return null;
		}
	}
}
